/*
 * @(#)SpringContextUtil.java
 *
 * Copyright (c) 2011, hp and/or its affiliates. All rights reserved.
 * HP PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.psammead.dms.common.util.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 获取Spring里注册的bean, 通过在配置文件中进行初始化
 * @author <a href="mailto:anjing.zhong@hp.com">Silence</a>
 * @version 1.0, Feb 9, 2011 2011
 * 
 */

public class SpringContextUtil implements ApplicationContextAware {
	
	private static ApplicationContext applicationContext; //Spring应用上下文环境

	/**
	 * 实现接口的回调方法, 设置上下文环境
	 */
	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		SpringContextUtil.applicationContext = arg0;
	}

	/**
	 * @return ApplicationContext
	 */
	public static ApplicationContext getApplicationContext(){
		return applicationContext;
	}
	
	/**
	 * 获取对象
	 * @param beanName
	 * @return Object 一个以所给名字注册的bean实例
	 */
	public static Object getBean(String beanName){
		return applicationContext.getBean(beanName);
	}
	
	/**
	 * 获取对象
	 * @param beanName
	 * @param  requireType对象类型
	 * @return requireType实例
	 */
	public static Object getBean(String beanName, Class<? extends Object> requireType){
		return applicationContext.getBean(beanName, requireType);
	}
	
	/**
	 * 获取上下文环境中所有bean的名称
	 * @return
	 */
	public static String[] getAllBeans(){
		return applicationContext.getBeanDefinitionNames();
	}
}
